<?php


namespace IDock\Insurance\ZMBX;

use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\ZMBX\Request\EPolicyDownloadRequest;
use IDock\Insurance\ZMBX\Request\LoginTokenRequest;
use IDock\Insurance\ZMBX\Request\NonCarProductListRequest;
use IDock\Insurance\ZMBX\Request\PolicyPaymentRequest;
use IDock\Insurance\ZMBX\Request\PremiumInquiryRequest;
use IDock\Insurance\ZMBX\Request\PremiumInquirySaveRequest;
use IDock\Insurance\ZMBX\Request\PremiumInquiryUpdateRequest;
use IDock\Insurance\ZMBX\Request\ProductClauseRequest;
use IDock\Insurance\ZMBX\Request\ProposalRecallRequest;
use IDock\Insurance\ZMBX\Request\ProposalStatusRequest;
use IDock\Insurance\ZMBX\Request\ProposalStatusUWRequest;
use IDock\Insurance\ZMBX\Request\RenewalInquiryRequest;
use IDock\Insurance\ZMBX\Request\RSAPublicKeyRequest;
use IDock\Insurance\ZMBX\Request\SpecialClausesRequest;
use IDock\Insurance\ZMBX\Request\UnderwritingRequest;
use IDock\Insurance\ZMBX\Request\UploadFileRequest;
use IDock\Insurance\ZMBX\Request\VehicleActualValueRequest;
use IDock\Insurance\ZMBX\Request\VehicleModelRequest;
use IDock\Insurance\ZMBX\Response\EPolicyDownloadResponse;
use IDock\Insurance\ZMBX\Response\LoginTokenResponse;
use IDock\Insurance\ZMBX\Response\NonCarProductListResponse;
use IDock\Insurance\ZMBX\Response\PolicyPaymentResponse;
use IDock\Insurance\ZMBX\Response\PremiumInquiryResponse;
use IDock\Insurance\ZMBX\Response\PremiumInquirySaveResponse;
use IDock\Insurance\ZMBX\Response\ProductClauseResponse;
use IDock\Insurance\ZMBX\Response\ProposalRecallResponse;
use IDock\Insurance\ZMBX\Response\ProposalStatusResponse;
use IDock\Insurance\ZMBX\Response\ProposalStatusUWResponse;
use IDock\Insurance\ZMBX\Response\RenewalInquiryResponse;
use IDock\Insurance\ZMBX\Response\RSAPublicKeyResponse;
use IDock\Insurance\ZMBX\Response\SpecialClausesResponse;
use IDock\Insurance\ZMBX\Response\UnderwritingResponse;
use IDock\Insurance\ZMBX\Response\UploadFileResponse;
use IDock\Insurance\ZMBX\Response\VehicleActualValueResponse;
use IDock\Insurance\ZMBX\Response\VehicleModelResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Printer\ProposalStatusPrinter;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

class ZMBXConstruct extends Builder implements IDockInterface
{
    public $config = [];

    public function __construct($config)
    {
        $this->config = $config;

        $this->secret = $config['secret'] ?? '';
        $this->proxy = $config['proxy'] ?? '';
        $this->passMsgUrl = $config['passMsgUrl'] ?? '';
        $this->callbackUrl = $config['callbackUrl'] ?? '';
    }


    /**
     * RSA 证书获取
     * @return RSAPublicKeyResponse
     * @version 4.0
     */
    public function rsaPublicKey(): RSAPublicKeyResponse
    {
        $request = new RSAPublicKeyRequest();
        $request->setUserCode($this->usercode);

        /*** @var $response RSAPublicKeyResponse */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 获取/刷新 token
     * @return LoginTokenResponse
     * @version 4.0
     */
    public function loginToken(): LoginTokenResponse
    {
        $request = new LoginTokenRequest();
        $request->setUserCode($this->usercode);
        $request->setPassWord($this->password);
        $request->setModule16($this->module16);
        $request->setExponent16($this->exponent16);
        $request->setPasswordRsANoPadding($this->passwordRsANoPadding);

        /*** @var $response LoginTokenResponse */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor)
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);

        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return Response
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor)
    {
        $response = new VehicleActualValueResponse();
        $response->setCode(200);
        $vehicleActualValuePrinter = new VehicleActualValuePrinter();
        $vehicleActualValuePrinter->actualValue = 0;
        $response->printer = $vehicleActualValuePrinter;
        return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return void
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        // TODO: Implement validateCar() method.
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return PremiumInquiryResponse
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): PremiumInquiryResponse
    {
        $request = new PremiumInquiryRequest;
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var PremiumInquiryResponse $response */
        $response = $this->execute($request);

        if (!$response->isSuccess()) {
            return $response;
        }

        if (!$response->printer->success) {
            return $response;
        }
        if($premiumInquiryVisitor->getVehicleVisitor()->isNewEnergy){
            $response->newEnergy();
        }

        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return UnderwritingResponse
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        //核保
        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var UnderwritingResponse $response */
        $response = $this->execute($request);
        $response->printer->inquiryNo = $premiumInquiryVisitor->getUnderwritingVisitor()->inquiryNo;
        return $response;
    }

    /**
     * 投保单状态查询
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return ProposalStatusResponse
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): ProposalStatusResponse
    {
        $request = new ProposalStatusRequest();
        $request->setVisitor($proposalStatusVisitor);

        /*** @var ProposalStatusResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 投保单撤单（删除）接口
     * @param ProposalRecallVisitor $revokeVisitor
     * @return ProposalRecallResponse
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): ProposalRecallResponse
    {
        $request = new ProposalRecallRequest();
        $request->setVisitor($revokeVisitor);

        /*** @var ProposalRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 支付
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor)
    {
        $response = new PolicyPaymentResponse(200);
        $response->url = $paymentVisitor->argc['payUrl'] ?? '';
        $response->printerInit();
        return $response;
    }

    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return Response
     */
    public function uploadFile(UploadFileVisitor $uploadFileVisitor)
    {
        $request =   new UploadFileRequest();
        $request->setVisitor($uploadFileVisitor);

        /*** @var UploadFileResponse $response */
        $response = $this->execute($request);
        return $response;
    }


    /**
     * 核保撤回
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return Response
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): Response
    {
        $request = new ProposalRecallRequest();
        $request->setVisitor($recallVisitor);

        /*** @var ProposalRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }


    /**
     * 电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return EPolicyDownloadResponse
     */
    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): EPolicyDownloadResponse
    {
        $request = new EPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var EPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 驾意险列表
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return Response
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): Response
    {

        $request = new NonCarProductListRequest();
        $request->setVisitor($nonCarProductListVisitor);
        /*** @var NonCarProductListResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter): Response
    {
        return new Response(200);
    }

    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): Response
    {
        return new Response(200);
    }
}
